﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orange.Lib.Effective;

namespace Orange.Lib.Effective
{
    namespace CompareUse
    {
        public class Customer : IComparable<Customer>, IComparable
        {
            private int level;

            public int Level
            {
                get { return level; }
                set { level = value; }
            }

            private string revenue;

            public string Revenue
            {
                get { return revenue; }
                set { revenue = value; }
            }


            public int CompareTo(Customer other)
            {
                return this.level.CompareTo(other.level);
            }

            int IComparable.CompareTo(object obj)
            {
                if (!(obj is Customer))
                    throw new ArgumentException(
                        "Argument is not a customer", "obj");
                return this.CompareTo((Customer)obj);
            }


            #region Operators //成对出现的操作符
            public static bool operator <(Customer left, Customer right)
            {
                return left.CompareTo(right) < 0;
            }

            public static bool operator >(Customer left, Customer right)
            {
                return left.CompareTo(right) > 0;
            }

            public static bool operator <=(Customer left, Customer right)
            {
                return left.CompareTo(right) <= 0;
            }

            public static bool operator >=(Customer left, Customer right)
            {
                return left.CompareTo(right) >= 0;
            }
            #endregion

            #region Collection.Sort(IComparer<Customer> / Comparion<Customer>)
            /*
                Customer c1 = new Customer { Level = 1 };
                Customer c2 = new Customer { Level = 2 };

                List<Customer> list = new List<Customer>();
                //IComparer<Customer>
                list.Sort(Customer.RevenueCompare);
                //Comparison<Customer>
                list.Sort(Customer.CompareByReview);
             */
            private static RevenueComparer revComp = null;
            public static IComparer<Customer> RevenueCompare
            {
                get
                {
                    if (revComp == null)
                        revComp = new RevenueComparer();
                    return revComp;
                }
            }

            public static Comparison<Customer> CompareByReview
            {
                get
                {
                    return (left, right) =>
                        left.revenue.CompareTo(right.revenue);
                }
            }

            private class RevenueComparer : IComparer<Customer>
            {
                int IComparer<Customer>.Compare(Customer left, Customer right)
                {
                    return left.revenue.CompareTo(
                        right.revenue);
                }
            }
            #endregion

        }
    }
}
